package com.kingdee.eas.custom.hr.attendance.app;

import java.sql.Connection;
import java.util.Calendar;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.common.Constants;
import com.kingdee.eas.custom.hr.attendance.CalendarFactory;
import com.kingdee.eas.custom.hr.attendance.CalendarInfo;
import com.kingdee.eas.custom.hr.attendance.CalendarTreeInfo;
import com.kingdee.eas.custom.hr.attendance.CalendarTypeFactory;
import com.kingdee.eas.custom.hr.attendance.CalendarTypeInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.TreeBaseInfo;
import com.kingdee.eas.util.app.DbUtil;

public class CalendarTreeControllerBean extends
		AbstractCalendarTreeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.attendance.app.CalendarTreeControllerBean");

	@Override
	protected IObjectPK _addnew(Context ctx, IObjectValue value)
			throws BOSException, EASBizException {
		CalendarTreeInfo tree = (CalendarTreeInfo) value;
		tree.setIsUse(true);
		IObjectPK pk = super._addnew(ctx, tree);

		if (tree.get("OPT") != null
				&& tree.get("OPT").toString().equals("COPY")) {
			return pk;
		}
		if (tree.getLevel() == 2) {
			CoreBaseCollection collection = this
					.getCalendarInfoList(ctx, value);
			CalendarFactory.getLocalInstance(ctx).addnew(collection);
		}

		return pk;
	}

	private CoreBaseCollection getCalendarInfoList(Context ctx,
			IObjectValue value) throws EASBizException, BOSException {
		CoreBaseCollection collection = new CoreBaseCollection();
		CalendarTreeInfo tree = (CalendarTreeInfo) value;
		CalendarTypeInfo type = this.getWeekendType(ctx);

		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, Integer.valueOf(tree.getYear()));
		c.set(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);

		int y1 = c.get(Calendar.YEAR);

		while (y1 == c.get(Calendar.YEAR)) {
			if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
					|| c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				CalendarInfo info = new CalendarInfo();
				info.setDate(c.getTime());
				info.setCalendarType(type);
				info.setTreeid((CalendarTreeInfo) value);
				collection.add(info);
			}
			c.add(Calendar.DATE, 1);
		}
		return collection;
	}

	private CalendarTypeInfo getWeekendType(Context ctx)
			throws EASBizException, BOSException {
		return CalendarTypeFactory.getLocalInstance(ctx).getCalendarTypeInfo(
				new ObjectUuidPK(Constants.CALENDAR_TYPE_WEEKEND));
	}

	protected void _update(Context ctx, IObjectPK pk, IObjectValue model)
			throws BOSException, EASBizException {
		TreeBaseInfo treeBaseInfo = (TreeBaseInfo) model;
		this._checkNumberDup(ctx, treeBaseInfo);
		Connection cn = getConnection(ctx);
		this.getDAO(ctx, cn).update(pk, model);
	}

	@Override
	protected void _delete(Context ctx, IObjectPK pk) throws BOSException,
			EASBizException {
		super._delete(ctx, pk);
		DbUtil.execute(ctx, "delete from ct_kq_calendar where ftreeid=?",
				new Object[] { pk.toString() });
	}
}